// pages/findCar/findCar.js
const app = getApp()
const constant = require('../../constant');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    carTypeShow:false,
    qdShow:false,
    matchShow:false,
    conditionKey: [],
    conditionType: 0,
    conditionDriver: 0,
    conditionBrand: 0,
    conditionFuel: 0,
    conditionDragweight: [],
    conditionWeight: [],
    conditionHorsepower: [],
    conditionIshot: 0,
    carList: [],
    truckDriver: [],
    pageNo: 1,
    pageCount: 10,
    listEnd: false
  },
  getTruckDriver: function() {
    let that = this;
    app.get('Truck/gettruckdriver', {
    }, function (res) {
      if (res.code == 200) {
        that.setData({
          truckDriver: res.data
        })
      }
    })
  },
  selCarDriver: function (e) {
    var item = e.currentTarget.dataset.item;
    var keydata = this.data.conditionKey.concat({ 'key': item.title });
    this.setData({
      conditionKey: keydata,
      conditionBrand: item.id,
      qdShow: false
    });
    this.getCarList();
  },
  selCarType: function (e) {
    var id = e.currentTarget.dataset.id;
    var key = '';
    if (id == '6') key = '牵引车';
    else if (id == '7') key = '载挂车';
    var keydata = this.data.conditionKey.concat({ 'key': key });
    this.setData({
      conditionKey: keydata,
      conditionBrand: id,
      carTypeShow: false
    });
    this.getCarList();
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getTruckDriver();
    let keylist = this.data.conditionKey;
    if (options.type == 'brand') {
      //品牌检索
      //设定筛选字段显示名称
      var key = JSON.parse(options.key);
      var data = JSON.parse(options.data);
      this.setData({ conditionKey: key });
      this.setData({ conditionBrand: data.brand });
    } else if (options.type == 'truckQyc') {
      //牵引车
      //设定筛选字段显示名称
      this.setData({ conditionKey: [{ 'key':'牵引车'}] });
      this.setData({ conditionType: 6 });
    } else if (options.type == 'truckZhc') {
      //载货车
      //设定筛选字段显示名称
      this.setData({ conditionType: 7 });
      this.setData({ conditionKey: [{'key':'载货车'}] });
    } else if (options.type == 'filter') {
      //筛选
      var data = JSON.parse(options.data);
      var fuel = [], dragweight = [], weight = [], horsepower = [], ishot = 0;
      for(var i=0; i<data.fuel.length; i++) {
        if(data.fuel[i] == 1) {
          fuel.push(i+1);
        }
      }
      for (var i=0; i<data.dragweight.length; i++) {
        if(data.dragweight[i] == 1) {
          dragweight.push(i+1);
        }
      }
      for (var i = 0; i < data.weight.length; i++) {
        if (data.weight[i] == 1) {
          weight.push(i + 1);
        }
      }
      for (var i = 0; i < data.horsepower.length; i++) {
        if (data.horsepower[i] == 1) {
          horsepower.push(i + 1);
        }
      }
      if (data.ishot.length > 1) {
        if (data.ishot[0] == 1) {
          ishot = 1;
        } else if (data.ishot[1] == 1) {
          ishot = 2;
        }
        //设定筛选条件
        this.setData({
          conditionFuel: fuel.join(','),
          conditionDragweight: dragweight.join(','),
          conditionWeight: weight.join(','),
          conditionHorsepower: horsepower.join(','),
          conditionIshot: ishot
        });
        var key = JSON.parse(options.key);
        //设定筛选字段显示名称
        this.setData({ conditionKey: key });
      }
    }
    
    this.getCarList();
  },
  getCarList: function (pageNo = 1) {
    let that = this;
    console.log('[findCar.js] getCarList fuel=' + that.data.conditionFuel);
    console.log('[findCar.js] getCarList dragweight=' + that.data.conditionDragweight);
    app.get('Goods/gettruckcatlist', {
      type: that.data.conditionType,
      driver: that.data.conditionDriver,
      brand: that.data.conditionBrand,
      fuel: that.data.conditionFuel,
      dragweight: that.data.conditionDragweight,
      weight: that.data.conditionWeight,
      horsepower: that.data.conditionHorsepower,
      ishot: that.data.conditionIshot,
      page_no: pageNo,
      page_count: that.data.pageCount
    }, function (res) {
      if (res.code == 200 && res.data.data_list.length > 0) {
        for(var i=0; i<res.data.data_list.length; i++) {
          res.data.data_list[i].img = constant.config.host + res.data.data_list[i].img;
        }
        that.setData({
          carList: that.data.carList.concat(res.data.data_list)
        })
        if (that.data.pageNo * that.data.pageCount >= res.data.total_count) {
          that.setData({ listEnd: true });
        }
      }
    })
  },
  toCartypeDetail: function (event) {
    var item = event.currentTarget.dataset.item;
    var id = item.titlecat_id;
    var imgTitle = item.img_title;
    var price = item.price;
    if (price=='' || price==null) price='暂无报价';
    var title = item.title;
    wx.navigateTo({
      url: '../carTypeDetail/carTypeDetail?id='+id+'&img='+imgTitle+'&title='+title+'&price='+price
    })
  },
  /**
   * 下拉显示更多
   */
  lower: function (e) {
    if (this.data.listEnd) return;
    var pageNo = this.data.pageNo + 1;
    this.setData({ pageNo: pageNo });
    this.getCarList(pageNo);
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  changeType(){
    this.setData({
      carTypeShow: !this.data.carTypeShow,
      qdShow:false
    })
  },
  changeQd(){
    this.setData({
      qdShow: !this.data.qdShow,
      carTypeShow: false
    })
  },
  openMatch(){
    this.setData({
      matchShow:true,
    })
  },
  closeMatch(){
    this.setData({
      matchShow: false,
    })
  },
  toFilter(){
    wx.navigateTo({
      url: '../filter/filter'
    })
  },
  closeDialog(){
    this.setData({
      qdShow: false,
      carTypeShow: false
    })
  },
  toBrand(){
    wx.navigateTo({
      url: '../brand/brand'
    })
  }
})